//package com.zsy.interceptor;
//
//import com.zsy.utils.JwtUtil;
//import com.zsy.utils.ThreadLocalUtil;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import java.util.Map;
//
//@Component
//public class LoginInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //浏览器发送实际请求前会先发送OPTIONS预检请求，实际发送了两次请求，第一次是options请求，第二次是get/post请求，第一次请求不会携带请求头参数，如果第一次请求就放行，就能获取到令牌
//        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
//            System.out.println("OPTIONS请求，放行");
//            return true;
//        }
//        String token = request.getHeader("Authorization");
//        System.out.println(token);
//        try{
//            Map<String, Object> claims = JwtUtil.parseToken(token);
//            ThreadLocalUtil.set(claims);
//            return true;
//        }catch (Exception e){
//            response.setStatus(401);
//            return false;
//        }
//    }
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        //清空ThreadLocal中的数据
//        ThreadLocalUtil.remove();
//    }
//}
